/**
 * 系统管理  公司管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true"  class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="searchname" placeholder="输入管理员名"></el-input>
      </el-form-item>
   
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="userName" label="管理员名" >
      </el-table-column>
      <el-table-column sortable prop="realName" label="姓名" >
      </el-table-column>
      <el-table-column sortable prop="phone" label="联系电话" >
      </el-table-column>
      <el-table-column sortable prop="email" label="邮箱" >
      </el-table-column>
      <el-table-column sortable prop="createDate" label="添加时间" >
        <template slot-scope="scope">
          <div>{{scope.row.createDate|timestampToTime}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" >
       <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="edit(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" >
      <el-form label-width="120px" :model="newlist"  >
         <el-form-item label="管理员名" prop="userName">
          <el-input size="small" v-model="newlist.userName" auto-complete="off" placeholder="请输入管理员名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="newlist.password" auto-complete="off" placeholder="请输入新密码"></el-input>
        </el-form-item>
         <el-form-item label="姓名" prop="realName">
          <el-input size="small" v-model="newlist.realName" auto-complete="off" placeholder="请输入新姓名"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input size="small" v-model="newlist.phone" auto-complete="off" placeholder="请输入联系电话"></el-input>
        </el-form-item>
         <el-form-item label="邮箱地址" prop="email">
          <el-input size="small" v-model="newlist.email" auto-complete="off" placeholder="请输入邮箱地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="dialogStatus == 'create' ? createData() : ConfirmUpdateStudent()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
   searchname:"",
         dialogStatus:"",
      newlist:{},
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id: '',
        userName: '',
        password: '',
        realName: '',
        phone: '',
        email: '',
        createDate:'',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
       
      },
      // 删除部门
  
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取公司列表
     getdata() {
      // this.loading = true
      this.loading = false
      // this.userData = res.data.data
      // // 分页赋值
     
      // 模拟数据结束
      axios.get("http://47.106.193.50:8080/admin/admins").then(res => {
           let data = res.data.data
           this.listData = data
           console.log(data,"5555");
            this.pageparm.currentPage = this.listData.page
      this.pageparm.pageSize = this.listData.limit
      this.pageparm.total = res.data.count
      }
      
      )
     
    },
      
    
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
      search() {
        let arr= []
        let list =  this.listData
       console.log(list,'323232');
       if (this.searchname == "") {
           this.getdata()
       }else{
        // console.log(this.searchname);
        for (let i = 0; i < list.length; i++) {
         if (list[i].userName.indexOf(this.searchname) >= 0) {
         arr.push(list[i])
      }}
        //  console.log(arr,'react');
         this.listData = arr
       }},
    //显示编辑界面
     // 添加
     add() {
    this.editFormVisible = true;
      this.dialogStatus = "create";
      console.log(this.dialogStatus,'添加');
      this.newlist = {}
    },
     // 控件
 createData() {
      console.log(this.dialogStatus,'添加');
  
     var data = this.newlist
     console.log(data,'add信息');
          axios.post("http://47.106.193.50:8080/addAdmin",data).then((response) => {
            console.log(response);
            if (response.data.code == "200") {
              this.editFormVisible = false;
              // this.fetchData();
              this.$notify({
                title: "添加成功",
                message: "edit Successfully",
                type: "success",
                duration: 2000,
              });
            } else {
              this.$notify({
                title: "添加失败",
                message: "edit failly",
                type: "warning",
                duration: 2000,
              });
            }
            this.getdata()
          });
        
     
    },
    //修改更新
   async edit(row) {
      this.newlist = { ...row };
      this.editFormVisible = true;
      console.log(this.newlist, "edit 信息");
      this.title = "修改"
      this.dialogStatus = "edit";
      console.log(this.dialogStatus,'修改');

    },
    async ConfirmUpdateStudent() {
      console.log(this.dialogStatus,'修改');
   
      await axios.post("http://47.106.193.50:8080/admin/updateAdmin",this.newlist).then((response) => {
        console.log(response);
        if (response.data.code == "200") {
          this.editFormVisible = false;
          this.$notify({
            title: "成功",
            message: "edit Successfully",
            type: "success",
            duration: 2000,
          });
        } else {
          this.$notify({
            title: "失败",
            message: "edit failly",
            type: "warning",
            duration: 2000,
          });
        }
      });
      this.getdata();
      this.editFormVisible = false;

      this.newlist = {};

    },

   
  
    // 删除

    handleDelete(row) {
      var data = row
    
      console.log(data);
      axios.post("http://47.106.193.50:8080/admin/deleteAdmin",data).then((response) => {
        console.log(response);
        if (response.data.code == "200") {
      
          this.getdata()
          this.$notify({
            
            title: "删除成功",
            message: "delete Successfully",
            type: "success",
            duration: 2000,
          });
        } else {
          this.$notify({
            title: "删除失败",
            message: "delete failly",
            type: "warning",
            duration: 2000,
          });
        }
        this.getdata()
      });
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 